/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.realriu.snipzel;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author riu
 */
public final class Settings {

    private Settings() {
    }

    public static final String LIBRARY_DIR = System.getProperty("user.home") + System.getProperty("file.separator") + "Library" + System.getProperty("file.separator") + "snipzel" + System.getProperty("file.separator");
    public static final String SETTINGS_FILE = LIBRARY_DIR + "settings";
    private static final Map<String, String> settings = new HashMap<String, String>();

    static {
        File sf = new File(SETTINGS_FILE);
        new File(LIBRARY_DIR).mkdirs();
        
        if (!sf.exists()) {
//            try {
                System.out.println("Write new settings file...");
                setSetting("check-intervall", "15000");
//            } catch (FileNotFoundException ex) {
//                ex.printStackTrace();
//                System.exit(1);
//            }
        } else {
            try {
                BufferedReader br = new BufferedReader(new FileReader(sf));
                String line;
                System.out.println("Settings:");
                while ((line = br.readLine()) != null) {
                    int sep = line.indexOf("=");
                    if (sep >= 0) {
                        settings.put(line.substring(0, sep), line.substring(sep + 1, line.length()));
                        System.out.println(line);
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static String getSetting(String settingKey) {
        return settings.get(settingKey);
    }

    public static void setSetting(String settingKey, String settingValue) {
        settings.put(settingKey, settingValue);
        try {
            File sf = new File(SETTINGS_FILE);
            System.out.println("Write settings file...");
            PrintStream ps = new PrintStream(sf);
            for(Map.Entry<String, String> e : settings.entrySet()){
                ps.println(e.getKey()+"="+e.getValue());
            }
            ps.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }
}
